import { wechatrequest } from './API/request'
import { getStorageSync } from '../crossplatform/apiservice/storage'

/**
 * 新增求职意向
 */
export const addCareerObjectiveRequest = ({ jobPlace, comprehensive, userId, userName }) => {
  return wechatrequest.postbody('/userArchive/addCareerObjective', {
    data: { jobPlace, userName, comprehensive, userId }
  })
}

/**
 * 编辑求职意向
 */
export const editCareerObjectiveRequest = ({ id, jobPlace, comprehensive, userId, userName }) => {
  return wechatrequest.putJson('/userArchive/c', {
    data: { id, jobPlace, comprehensive, userId, userName }
  })
}

/**
 * 修改求职状态
 */
export const updateJobStatus = ({ archiveId, jobStatus }) => {
  const userinfo = getStorageSync('userinfo')
  const userId = userinfo.userId
  return wechatrequest.put('/userArchive/updateJobStatus', {
    data: { archiveId, jobStatus, userId }
  })
}

/**
 * 删除求职意向
 */
export const deljobHuntingRequest = ({ id, userId }) => {
  return wechatrequest.delete('/userArchive/deljobHunting', {
    data: { id, userId }
  })
}

/**
 * 新增个人风采照片
 */
export const insertImgRequest = ({ imgUrl }) => {
  const userinfo = getStorageSync('userinfo')
  const userId = userinfo.userId
  return wechatrequest.postparams('/userArchive/insertImg', {
    data: { imgUrl, userId }
  })
}

/**
 * 删除个人风采照片
 */
export const delImgRequest = ({ ids }) => {
  const userinfo = getStorageSync('userinfo')
  const userId = userinfo.userId
  return wechatrequest.delete('/userArchive/delImg', {
    data: { ids, userId }
  })
}

/**
 * 获取岗位分类列表
 */
export const getIndustryCategoryRequest = type => {
  return wechatrequest.postparams('/common/getIndustryCategory', {
    data: { type }
  })
}

/**
 * 在招职位分类
 */
export const inRecruitPostCategoryRequest = ({ type = 0 }) => {
  return wechatrequest.postform('@@/post/inRecruitPostNameCategoryList', {
    data: { type }
  })
}

/**
 * 获取简历信息
 */
export const getUserArchiveInfoRequest = ({ userId }) => {
  return wechatrequest.get('/userArchive/getUserArchiveInfo', {
    data: { userId }
  })
}

/**
 * 人才是否已被收藏
 */
export const getAlike = ({ userId, archiveId }) => {
  return wechatrequest.postparams(`/userArchive/findUserArchiveByArchiveId`, {
    data: {
      userId: userId,
      archiveId
    }
  })
}

/**
 * 新增教育信息
 */
export const insertEducationRequest = ({
  attributes,
  beginTime,
  degreeLevel,
  endTime,
  majorName,
  schoolName
}) => {
  const userinfo = getStorageSync('userinfo') || {}
  const userId = userinfo.userId
  return wechatrequest.postparams('/userArchive/insertEducation', {
    data: {
      attributes,
      beginTime: beginTime + '-01-01 00:00:00',
      degreeLevel,
      endTime: endTime + '-01-01 00:00:00',
      majorName,
      schoolName,
      userId: userId * 1
    }
  })
}

/**
 * 新增工作经历信息
 */
export const insertWorkRequest = ({
  achievement,
  beginTime,
  companyName,
  endTime,
  positionName
}) => {
  const userinfo = getStorageSync('userinfo') || {}
  const userId = userinfo.userId
  return wechatrequest.postbody('/userArchive/insertWork', {
    data: { userId, achievement, beginTime, companyName, endTime, positionName }
  })
}

/**
 * 编辑工作经历信息
 */
export const updateWorkRequest = ({
  id,
  achievement,
  beginTime,
  companyName,
  endTime,
  positionName
}) => {
  const userinfo = getStorageSync('userinfo')
  return wechatrequest.putJson('/userArchive/updateWork', {
    data: {
      id: id - 0,
      achievement,
      beginTime,
      companyName,
      endTime,
      positionName,
      userId: userinfo.userId - 0
    }
  })
}

/**
 * 编辑教育信息
 */
export const updateEducationRequest = ({
  id,
  attributes,
  beginTime,
  degreeLevel,
  endTime,
  majorName,
  schoolName
}) => {
  const userinfo = getStorageSync('userinfo') || {}
  const userId = userinfo.userId
  let params = {}
  if (attributes) {
    params['attributes'] = attributes
  }
  if (beginTime) {
    params['beginTime'] = beginTime + '-01-01 00:00:00'
  }
  if (degreeLevel) {
    params['degreeLevel'] = degreeLevel
  }
  if (endTime) {
    params['endTime'] = endTime + '-01-01 00:00:00'
  }
  if (majorName) {
    params['majorName'] = majorName
  }
  if (schoolName) {
    params['schoolName'] = schoolName
  }
  if (userId) {
    params['userId'] = userId
  }
  return wechatrequest.put('/userArchive/updateEducation', {
    data: {
      id: id - 0,
      ...params
    }
  })
}

/**
 * 首页人才简历列表
 */
export const queryArchiveForPageRequest = ({
  education,
  page = 1,
  pageSize = 10,
  category,
  type,
  area,
  isOld,
  sortType
}) => {
  let jobPlace = (getStorageSync('landmark') && getStorageSync('landmark').landmarkName) || '上海'
  const userinfo = getStorageSync('userinfo')
  const userId = userinfo.zhichuUserIdB || ''
  let params = {
    isOld,
    education,
    jobPlace,
    pageNum: page,
    pageSize,
    workCategory: category,
    workNature: type,
    workPlace: area,
    userId
  }
  if (sortType) params.sortType = sortType
  return wechatrequest.putJson('/userArchive/queryArchiveForPage', {
    data: params
  })
}

/**
 * 搜索人才
 */
export const queryArchiveRequest = ({ limit = 10, page = 1, query }) => {
  let jobPlace = (getStorageSync('landmark') && getStorageSync('landmark').landmarkName) || '上海'
  let params = {
    jobPlace,
    limit,
    page,
    query
  }
  return wechatrequest.postparams('/userArchive/queryArchive', {
    data: params
  })
}

/**
 * 删除教育信息
 */
export const putDeleteEducationRequest = ({ id, userId }) => {
  return wechatrequest.put('/userArchive/deleteEducation', {
    data: {
      id,
      userId
    }
  })
}

/**
 * 删除工作经历
 */
export const putDeleteWorkExperienceRequest = ({ id, userId }) => {
  return wechatrequest.put('/userArchive/deleteWorkExperience', {
    data: { id, userId }
  })
}

/**
 * 上传头像
 */
export const putUploadImgRequest = ({ imgUrl, type, userId }) => {
  return wechatrequest.put('/userArchive/uploadImg', {
    data: { imgUrl, type, userId }
  })
}
